<?php

namespace Helpers;

use Phalcon\Di;

require_once('simple_html_dom.php');
class Template {

    /**
     * 返回模板文件及配置文件根目录
     *
     * @return string
     *
     * create by ck 20160307
     */
    public static function getPath()
    {
        return APP_PATH . '/modules/' . Di::getDefault()->get('dispatcher')->getModuleName() ."/template";
    }

    /**
     * 根据模板类型查看模板是否存在，存在返回true，不存在返回false
     *
     * @param $name     --模板名称
     * @param $type     --类型
     * @return bool     --返回查询结果
     *
     * create by ck 20160105
     * modify by ck 20160813    增加'both'类型，如为both则检查是否两个都存在
     */
	public static function exists($name, $type){
        $templatePath = self::getPath();
        switch($type){
            case 'form' : return file_exists($templatePath . '/form/' . $name . '.html') ? true : false; break;
            case 'list' : return file_exists($templatePath . '/list/' . $name . '.xml')  ? true : false; break;
            case 'both' : return (file_exists($templatePath . '/list/' . $name . '.xml') && file_exists($templatePath . '/form/' . $name . '.html')) ? true : false; break;
            default: return false;
        }
	}

    /***********************************************************************************
     * 根据模板名称返回模板内容，如果下拉框定义enum属性，则从数据库查询信息添加到下拉框
     *
     * @param $form_name                --模板名称
     * @param array $enumArr            --下拉框添加选项
     * @return string                   --表单内容
     *
     * create by ck 20160106
     * modify by ck 20160316    如果下拉框定义enum属性，则从数据库查询信息添加到下拉框
     * modify by ck 20160822    enum结构更改
     * modify by ck 20160831    防止内存泄露
     * modify by ck 20161008    枚举查询更改
     ***********************************************************************************/
    public static function createForm($form_name, $enumArr = []){
        $templatePath = self::getPath();
        $contents = file_get_html($templatePath . '/form/' . $form_name . '.html', false, null, 0);
        $enumList = EnumList::getFieldEnumList();
        foreach($contents->find('select[enum]') as $field){
            if($field->enum === true) {
                $fieldName = $field->name;
                $fieldName = explode(',', $fieldName);
                $fieldName = end($fieldName);
                if(isset($enumList[$fieldName])){
                    foreach ($enumList[$fieldName] as $key => $value) {
                        $field->innertext .= "<option value='{$key}'>{$value}</option>";
                    }
                }
            }
            else{
                $enums = \Admin\Models\sys\XhsysEnum::getEnumByNodeId($field->enum);
                if (!$enums) continue;
                foreach ($enums as $key => $value) {
                    $field->innertext .= "<option value='{$key}'>{$value}</option>";
                }
            }
        }

        //填充传入枚举数组
        foreach($enumArr as $field => $enum) {
            $dom = $contents->find("select[name$={$field}]", 0);
            if(!$dom) continue;
            foreach($enum as $key => $value){
                $dom->innertext .= "<option value='{$key}'>{$value}</option>";
            }
//            foreach ($contents->find("select[name$={$field}]") as $dom){
//                foreach($enum as $key => $value){
//                    $dom->innertext .= "<option value='{$key}'>{$value}</option>";
//                }
//            }
        }
        $form = $contents->outertext;
        $contents->clear();
        unset($contents);
        return $form;
    }

    /**
     * 根据模板名称和内容数据，在原模板内添加数据后返回
     *
     * @param $form_name        --模板名称
     * @param $data             --内容数据
     * @return string
     *
     * create by ck 20160111
     * modify by ck 20160831    防止内存泄露
     * modify by ck 20170308    增加number及date类型
     */
    public static function createFormWithData($form_name, $data){
        $contents = str_get_html(self::createForm($form_name));
        if($contents->find('input[type=text]')){                        //普通文本框
            foreach($contents->find('input[type=text]') as $field){
                self::inputWithData($field, $data);
            }
        }
        if($contents->find('input[type=number]')){
            foreach($contents->find('input[type=number]') as $field){
                self::inputWithData($field, $data);
            }
        }
        if($contents->find('input[type=date]')){
            foreach($contents->find('input[type=date]') as $field){
                self::inputWithData($field, $data);
            }
        }
        if($contents->find('input[type=radio]')){                       //单选框组，如选中则跳出循环
            foreach($contents->find('input[type=radio]') as $field){
                if(self::radioWithData($field, $data)) break;
            }
        }
        if($contents->find('input[type=checkbox]')){                    //多选框组
            foreach($contents->find('input[type=checkbox]') as $field){
                self::checkboxWithData($field, $data);
            }
        }
        if($contents->find('textarea')){                                //多行文本框
            foreach($contents->find('textarea') as $field){
                self::textareaWithData($field, $data);
            }
        }
        if($contents->find('select')){                                  //下拉框
            foreach($contents->find('select') as $key => $field){
                //self::selectWithData($field, $data);
                $fieldName = explode(',', $field->name);
                $select = str_get_html($field);
                foreach($select->find('option') as $option){
                    if($option->value == $data[$fieldName[1]]){
                        $option->selected = 'selected';
                        break;
                    }
                }
                $contents->find('select', $key)->outertext = $select->outertext;
                $select->clear();
                unset($select);
            }
        }
        $form = $contents->outertext;
        $contents->clear();
        unset($contents);
        return $form;
    }

    /**
     * 将input元素加上内容
     *
     * @param $field
     * @param $data
     *
     * create by ck 20160115
     */
    static function inputWithData($field, $data){
        $fieldName = explode(',', $field->name);
        if(isset($data[$fieldName[1]])){
            $field->value = $data[$fieldName[1]];
        }
    }

    /**
     * 将radio元素选中相应选项，如果找到选中项则返回真
     *
     * @param $field
     * @param $data
     * @return bool     --成功标识
     *
     * create by ck 20160118
     */
    static function radioWithData($field, $data){
        $fieldName = explode(',', $field->name);
        if($field->value == $data[$fieldName[1]]){
             $field->checked = 'checked';
            return true;
        }
        return false;
    }

    /**
     * 将checkbox元素选中相应选项，多选按照逗号(,)分隔
     *
     * @param $field
     * @param $data
     *
     * create by ck 20160118
     */
    static function checkboxWithData($field, $data){
        $fieldName = explode(',', $field->name);
        $values = explode(',', $data[$fieldName[1]]);
        if(in_array($field->value, $values)){
            $field->checked = 'checked';
        }
    }

    /**
     * 将textarea元素加上内容
     *
     * @param $field
     * @param $data
     *
     * create by ck 20160118
     */
    static function textareaWithData($field, $data){
        $fieldName = explode(',', $field->name);
        if(isset($data[$fieldName[1]])){
            $field->innertext = $data[$fieldName[1]];
        }
    }

    /*********************************************************************************
     * 根据xml文件名读取文件后返回内容
     * @param $xmlName              --xml文件名
     * @return \SimpleXMLElement     --xml文档内容
     *
     * create by ck 20160106
     *********************************************************************************/
    public static function readXmlInfo($xmlName){
        $templatePath = self::getPath();
        $xml = simplexml_load_file($templatePath . '/list/' . $xmlName . '.xml');
        return $xml;
    }

    /**
     * 根据xml文件名获取字段及说明
     *
     * @param $xmlName
     * @param $ignoreHidden
     * @return array
     *
     * create by ck 20180227
     */
    public static function getXmlFields($xmlName, $ignoreHidden = true) {
        $fields = self::readXmlInfo($xmlName)->header->field;
        $title  = [];
        foreach ($fields as $field) {
            $field = (array)$field;
            if ($ignoreHidden && isset($field['@attributes']['hidden']) && $field['@attributes']['hidden'] == 'true') {
                continue;
            }
            $title[$field[0]] = isset($field['@attributes']['title']) ? $field['@attributes']['title'] : '';
        }
        return $title;
    }

    /***********************************************************
     * 根据配置信息生成相应的toolbar并返回
     *
     * @param $params                       --配置信息
     * @param bool|false $hr                --是否生成分割横线
     * @return string                       --内容
     *
     * create by ck 20160823
     ***********************************************************/
    public static function createSearchToolBar($params, $hr = false){
        $toolBar = '';
        $checkButton = false;
        foreach($params as $param){
            switch($param['type']){
                case 'text':   unset($param['type']); $toolBar .= self::createTextElement($param); break;          //创建文本框
                case 'button': unset($param['type']); $toolBar .= self::createButtonElement($param); $checkButton = true; break;        //创建button按钮
                case 'select': unset($param['type']); $toolBar .= self::createSelectElement($param); break;        //创建下拉框
            }
        }
        if(!$checkButton) $toolBar .= self::createButtonElement(['id'=>'bt_list_search', 'text'=>'查找', 'icon'=>'fa-search', 'style'=>'margin-left:10px']);
        if($hr) $toolBar .= '<div class="datagrid-toolbar" style="margin:3px 0 5px -10px;width:110%;"></div>';
        return $toolBar;
    }

    /**
     * 根据配置数组循环生成toolBar
     *
     * @param array $params
     * @return string
     *
     * create by ck 20160729
     */
    public static function createButtonToolBar($params = array()){
        $toolBarStr = '';
        foreach($params as $param) {
            $toolBarStr .= self::createButtonElement($param);
        }
        return $toolBarStr;
    }

    /**
     * 根据配置信息和html对象生成按钮并返回对象
     *
     * @param $param
     * @return string $buttonStr
     *
     * create by ck 20160126
     * modify by ck 20160729    优化，问题仍然存在--icon只能最后处理，否则i标签消失
     * modify by ck 20160831    防止内存泄露
     */
    static function createButtonElement($param){
        $button = str_get_html('<a></a>');              //添加a为标签的按钮
        self::createBaseAttribute($param, $button);     //添加基础属性
        foreach($param as $key => $value){
            $button->lastChild()->plain = 'true';       //默认都添加
            switch($key){
//                case 'plain'    : $button->lastChild()->plain     = $value; break;     //鼠标移动到按钮上方显示边框
                case 'event'    : $button->lastChild()->onclick    = $value; break;     //添加按钮事件
                case 'disabled' : $button->lastChild()->disabled   = $value; break;     //禁用
                case 'text'     : $button->lastChild()->innertext .= $value; break;     //添加按钮显示的文本
                case 'icon'     : $button->lastChild()->innertext  = '<i class="fa '.$value.' fa-lg fa-fw" style="color:#2baab1;"></i>'
                    . $button->lastChild()->innertext; break;                          //添加图标
            }
        }
        $buttonStr = $button->outertext;
        $button->clear();
        unset($button);
        return $buttonStr;
    }

    /**
     * 根据配置信息和html对象添加基础属性
     *
     * @param $param    --配置信息
     * @param $dom      --dom对象
     *
     * create by ck 20160119
     * modify by ck 20160831
     * modify by ck 20160906    添加label属性后重新加载dom元素
     */
    static function createBaseAttribute(&$param, &$dom){
        foreach($param as $key => $value){
            switch($key){
                case 'id': //添加id属性
                    $dom->lastChild()->id = $value;
                    unset($param[$key]);
                    break;
                case 'name': //添加name属性
                    $dom->lastChild()->name = $value;
                    unset($param[$key]);
                    break;
                case 'label': //元素前添加文本
                    $dom->lastChild()->outertext = $value . $dom->lastChild()->outertext;
                    unset($param[$key]);
                    $tmp = $dom->outertext;
                    $dom->clear();
                    $dom = str_get_html($tmp);
                    break;
                case 'style': //添加style属性
                    $dom->lastChild()->style = $dom->lastChild()->style . ' ' . $value;
                    unset($param[$key]);
                    break;
                case 'class': //添加class属性
                    $dom->lastChild()->class = $dom->lastChild()->class . ' ' . $value;
                    unset($param[$key]);
                    break;
            }
        }
    }

    /**
     * 根据配置信息和html对象添加文本框并返回对象
     *
     * @param $param
     * @return string
     *
     * create by ck 20160119
     *
     * modify by ck 20160902    结构更改
     * modify by ck 20180517    默认135px宽度
     */
    static function createTextElement($param){
        $textDom = str_get_html('<input type="text"/>');      //添加text
        $textDom->lastChild()->style = 'line-height:30px;';
        self::createBaseAttribute($param, $textDom);          //添加基础属性
        foreach($param as $key => $value){
            $textDom->lastChild()->$key = $value; break;
//                switch($key){
//                    case 'prompt' : $textDom->lastChild()->options = "{$key}:'{$value}'"; break;    //添加提示信息
//                    case 'default': $textDom->lastChild()->value = $value; break;                   //添加默认值
//                }
        }
        if (!array_key_exists('style', $param)) {
            $textDom->lastChild()->style .= 'width: 135px;';
        }
        $textElement = $textDom->outertext;
        $textDom->clear();
        unset($textDom);
        return $textElement;
    }

    /**
     * 根据配置信息添加下拉框并返回
     *
     * @param $param
     * @return string
     *
     * create by ck 20160902
     */
    static function createSelectElement($param){
        if(isset($param['data'])){
            $option = '';   //下拉选项
            foreach($param['data'] as $key => $value){
                if(isset($param['default']) && $key == $param['default']){
                    $option .= "<option value='{$key}' selected='selected'>{$value}</option>";
                }
                else{
                    $option .= "<option value='{$key}'>{$value}</option>";
                }
            }
            $selectDom = str_get_html('<select>'.$option.'</select>');
        }
        else {
            $selectDom = str_get_html('<select></select>');          //添加select
        }
        self::createBaseAttribute($param, $selectDom);            //添加基础属性
        foreach($param as $key => $value){
            switch($key){
                case 'event':       $selectDom->lastChild()->onchange = $value; break;          //添加onchange事件
                case 'placeholder': $selectDom->lastChild()->placeholder = $value; break;
                case 'multiple':    $selectDom->lastChild()->multiple = $value; break;
                case 'dataUrl': {
                    if(!isset($param['data'])&&isset($param['valueField'])&&isset($param['textField'])){
                        $selectDom->lastChild()->setAttribute('data-options', "valueField:'{$param['valueField']}',textField:'{$param['textField']}',url:'{$value}'");
                    }
                }break;
            }
        }
        return $selectDom;
    }

    /******************************************************************************
     * 根据配置文件，读取出头文件配置及字段信息，组成easyui datagrid格式table并返回
     * 数组参数可扩展添加属性
     *
     * @param $id                               --列表ID
     * @param $xmlName                          --配置文件名
     * @param array $params                     --数组参数
     * @return string
     *
     * create by ck 20160722
     * modify by ck 20160810    如果有$params['field']，则限制xml中字段，只显示参数中存在的字段
     * modify by ck 20160831    防止内存泄露
     * modify by ck 20170601    添加默认表头样式
     * modify by ck 20171130    加入冻结列
     ******************************************************************************/
    public static function createList($id, $xmlName, $params = array()){
        $xml    = self::readXmlInfo($xmlName);
        $table  = str_get_html('<table id="'.$id.'" toolbar="#'.$id.'_toolbar" style="height:100%"></table>');
        $table->find('table', 0)->headerCls = 'xh-datagrid-header';
        if($xml->config){
            foreach($xml->config->children() as $key => $value){
                $table->find('table', 0)->$key = $value;
                if(isset($params['title'])) $table->find('table',0)->title = $params['title'];
            }
        }
        if(isset($params['tableAttr'])) {
            foreach ($params['tableAttr'] as $attr => $value) {
                $table->find('table', 0)->$attr = $value;
            }
        }
        $frozenHead = '<thead data-options="frozen:true"><tr>';
        $tableHead  = '<thead><tr>';
        if(!isset($params['checkbox']) || $params['checkbox'] !== false){
            $frozenHead .= "<th data-options=\"field:'ck',checkbox:true\"></th>";
        }
        if($xml->header->field){                                                //根据header内属性决定表头
            foreach ($xml->header->field as $field) {
                $isFrozen = false;
                if($field && (isset($params['field']) ? in_array($field, $params['field']) : true)){
                    $fieldStr = "<th data-options=\"field:'{$field}'";
                    foreach($field->attributes() as $key => $value){
                        if ($key == 'frozen' && $value == 'true') {
                            $isFrozen = true;
                            continue;
                        }
                        if($key != 'title') $fieldStr .= ",{$key}:'{$value}'";
                    }
                    $fieldStr .= '">';
                    $fieldStr .= $field->attributes()->title ? $field->attributes()->title : $field;
                    $fieldStr .= '</th>';
                }
                else{
                    continue;
                }
                if ($isFrozen) {
                    $frozenHead .= $fieldStr;
                }
                else {
                    $tableHead .= $fieldStr;
                }
            }
        }
        else{                                                                   //未指定则报错
            $tableHead .= "<th data-options=\"field:'xx',width:'300'\">";
            $tableHead .= '开发中，敬请期待...';
            $tableHead .= "</th>";
        }
        $tableHead  .= '</tr></thead>';
        $frozenHead .= '</tr></thead>';
//        if ($frozenHead != '<thead data-options="frozen:true"><tr></tr></thead>') {
            $tableHead = $frozenHead . $tableHead;
//        }
        $table->find('table',0)->innertext = $tableHead;
        $tableStr = $table->outertext;
        $table->clear();
        unset($table);
        if(isset($params['searchToolbar']) || isset($params['buttonToolbar'])){
            $tableStr = '<div id="'.$id.'_toolbar" class="xh-datagrid-toolbar" style="line-height:20px;padding:5px 0 5px 10px;">'
            . (isset($params['searchToolbar']) ? $params['searchToolbar'] : '')
            . (isset($params['buttonToolbar']) ? $params['buttonToolbar'] : '')
            . '</div>' . $tableStr;
        }
        return $tableStr;
    }

    /***********************************************************
     * @param $jsons                        --配置信息
     * @param string $hr                    --是否生成分割横线
     * @return bool|simple_html_dom html    --内容
     * 根据配置信息生成相应的toolbar并返回
     * create by ck 2016/1/22
     ***********************************************************/
    /*public static function createSearchToolBar($jsons, $hr="false"){
        try{
            $jsons=json_decode($jsons);
            $toolBar=str_get_html("");
            $has_button=0;//是否有button
            foreach($jsons as $json){
                switch($json->type){
                    case "text":$toolBar=self::createTextElement($json, $toolBar);break;//创建文本框
                    case "button":$toolBar=self::createButtonElement($json, $toolBar);$has_button=1;break;//创建button按钮
                    case "select":$toolBar=self::createSelectElement($json, $toolBar);break;//创建下拉框
                }
            }
            if($has_button==0){//没有按钮则创建
                $json='{"type":"button","id":"bt_search","name":"bt_search","plain":"true","icon":"icon-search","style":"margin-left:10px;","text":"查询"}';
                $json=json_decode($json);
                $toolBar=self::createButtonElement($json, $toolBar);
            }
            if($hr=="true"){
                $toolBar=$toolBar."<div class=\"datagrid-toolbar\" style=\"margin-left:-10px;width:110%\"></div>";
            }
            return $toolBar;
        }
        catch(\Phalcon\Exception $e){
            Main::createErrorLog(__METHOD__, $e->getMessage());
        }
    }*/

    /**
     * @param $id
     * @param $xmlName
     * @param string $search_toolbar
     * @param string $button_toolbar
     * @return bool|simple_html_dom|string
     * 根据id，xml名，toolbar字符串生成表格及工具栏
     * create by ck 2016/1/27
     * 增加$conditions参数，有此参数则添加在url后
     * modify by ck 2016/3/4
     * 删除$conditions参数，添加$ck，有此参数隐藏复选框
     * modify by ck 2016/3/18
     */
    /*public static function createList($id, $xmlName, $search_toolbar="", $button_toolbar="", $title="", $ck=""){
        try{
            $xml=self::readXmlInfo($xmlName);
            $table=str_get_html('<table id="'.$id.'" toolbar="#'.$id.'_toolbar" style="height:100%"></table>');
            foreach($xml->config->children() as $key => $value){
                if($key=="title" && $title!=""){
                    $table->find("table",0)->$key=$title;
                }
                else{
                    $table->find("table",0)->$key=$value;
                }
            }
            $thead="<thead><tr>";
            if($ck!="display"){
                $thead.="<th data-options=\"field:'ck',checkbox:true\"></th>";
            }
            if($xml->header->field){//根据header内属性决定表头
                foreach ($xml->header->field as $field) {
                    if($field){
                        $field_str="<th data-options=\"field:'{$field}'";
                        foreach($field->attributes() as $key => $value){
                            if($key!="title"){
                                $field_str.=",{$key}:'{$value}'";
                            }
                        }
                        $field_str.="\">";
                        if($field->attributes()->title){
                            $field_str.=$field->attributes()->title;
                        }
                        else{
                            $field_str.=$field;
                        }
                        $field_str.="</th>";
                    }
                    else{
                        continue;
                    }
                    $thead.=$field_str;
                }
            }
            else{//未指定则报错
                $thead.="<th data-options=\"field:'xx',width:'300'\">";
                $thead.="配置文件出错 请检查";
                $thead.="</th>";
            }
            $thead.="</tr></thead>";
            $table->find("table",0)->innertext=$thead;
            if($search_toolbar!="" || $button_toolbar!=""){
                $table="<div id='".$id."_toolbar' class='xh-datagrid-toolbar' style='line-height:35px;padding-left:10px;'>"
                    .$search_toolbar.$button_toolbar."</div>".$table;
            }
            return $table;
        }
        catch(\Phalcon\Exception $e){
            Main::createErrorLog(__METHOD__, $e->getMessage());
        }
    }*/

    /*static function createSelectElement($param){
        try{
            if(isset($param['data'])){
                $option="";//下拉选项
                foreach($param['data'] as $key => $value){
                    if(isset($param['default']) && ($key == $param['default'])){
                        $option.='<option value="'.$key.'" selected="selected">'.$value.'</option>';
                    }
                    else{
                        $option.='<option value="'.$key.'">'.$value.'</option>';
                    }
                }
                $toolBar=str_get_html('<select>'.$option.'</select>');
            }
            else{
                $toolBar=str_get_html('<select></select>');//添加select
            }
            self::createBaseAttribute($param, $toolBar);//添加基础属性
//            $toolBar=str_get_html($toolBar);
//            $toolBar->lastChild()->class="easyui-combobox";
//            $toolBar->lastChild()->editable="false";//设置下拉框为不可编辑
//            $toolBar->lastChild()->panelHeight="auto";//设置下拉框高度自动
            if(isset($param['multiple'])){
                $toolBar->lastChild()->multiple = "true";//设置为多选
            }
            foreach($param as $key => $value){
                switch($key){
                    case "event":   $toolBar->lastChild()->onchange=$value;break;//添加onchange事件
                    case "dataUrl": {
                        if(!isset($param['data'])&&isset($param['valueField'])&&isset($param->textField)){
                            $toolBar->lastChild()->setAttribute("data-options","valueField:'{$param->valueField}',textField:'{$param->textField}',url:'{$value}'");
                        }
                    }break;
                }
            }
            return $toolBar;
        }
        catch(\Phalcon\Exception $e){
            Main::createErrorLog(__METHOD__, $e->getMessage());
        }
    }*/

    /**
     * @param $param
     * @param $toolBar
     * @return bool|simple_html_dom
     * 根据配置信息和html对象添加button按钮并返回对象
     * create by ck 2016/1/19
     */
    /*static function createButtonElement($param, $toolBar){
        try{
            $toolBar=str_get_html($toolBar.'<input type="button"/>');//添加button
            $toolBar->lastChild()->class="easyui-linkbutton";
            self::createBaseAttribute($param, $toolBar);//添加基础属性
            foreach($param as $key => $value){
                switch($key){
                    case "text":$toolBar->lastChild()->value=$value;break;//添加按钮显示的文本
                    case "event":$toolBar->lastChild()->onclick=$value;break;//添加按钮事件
                    case "plain":$toolBar->lastChild()->plain=$value;break;
                    case "icon":$toolBar->lastChild()->iconCls=$value;break;
                }
            }
            return $toolBar;
        }
        catch(\Phalcon\Exception $e){
            Main::createErrorLog(__METHOD__, $e->getMessage());
        }
    }*/

    /**
     * @param $jsons
     * @param $div
     * @return bool|simple_html_dom
     * 根据配置信息生成相应的buttontoolbar并返回
     * create by ck 2016/1/20
     */
    /*public static function createButtonToolBar($jsons, $div="{}"){
        try{
            $jsons=json_decode($jsons);
            $toolBar=str_get_html("");
            foreach($jsons as $json){
                $toolBar=self::createButtonElement($json, $toolBar);
            }
            $div=json_decode($div);
            if(count((array)$div)){
                $toolBar=str_get_html("<div class=\"datagrid-toolbar\">".$toolBar."</div>");
                foreach($div as $key => $value){
                    $toolBar->find("div",0)->$key=$value;
                }
            }
            return $toolBar;
        }
        catch(\Phalcon\Exception $e){
            Main::createErrorLog(__METHOD__, $e->getMessage());
        }
    }*/


    /**
     * @param $field
     * @param $data
     * 将select元素选中相应选项
     * create by ck 2016/1/18
     */
    /*static function selectWithData($field, $data){
        try{
            $fieldName = explode(",", $field->name);
            $field = str_get_html($field);
            $field->find('option[value='.$data[$fieldName[1]].']')->selected = 'selected';
//            foreach($field->find('option') as $option){
//                if($option->value == $data[$fieldName[1]]){
//                    $option->selected="selected";
//                    break;
//                }
//            }
        }
        catch(\Phalcon\Exception $e){
            Main::createErrorLog(__METHOD__, $e->getMessage());
        }
    }*/

}